//
//  HotelDetailHeaderView.m
//  Traveling
//
//  Created by 尹桥印 on 15/6/10.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "HotelDetailHeaderView.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "DateSelect.h"
#import "CustomShowView.h"
#import "SelectDateView.h"
#import "ImagePlayerView.h"

#define FMTSTR @"yyyy-MM-dd"

@interface HotelDetailHeaderView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bannerView;

@property (weak, nonatomic) IBOutlet UILabel *hotelNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *telLabel;
@property (nonatomic, weak) CustomShowView *showView;
@property (weak, nonatomic) IBOutlet UILabel *totalCountLabel;

@property (weak, nonatomic) IBOutlet UIButton *startBtn;

@property (weak, nonatomic) IBOutlet UIButton *endBtn;


@property (nonatomic, weak) UIPageControl *pageControl;




@end

@implementation HotelDetailHeaderView

+(instancetype)headerView{
    return [[[NSBundle mainBundle] loadNibNamed:@"HotelDetailHeaderView" owner:nil options:nil] lastObject];
}

-(void)awakeFromNib{
    
    self.width = SCREENW;
    _dayCount = 1;
    NSDate *startDate = [NSDate date];
    [self.startBtn setTitle:[self stringWithDate:startDate] forState:UIControlStateNormal];
    _startDate = startDate;
    
    NSDate *endDate = [startDate dateByAddingTimeInterval:60*60*24];
    [self.endBtn setTitle:[self stringWithDate:endDate] forState:UIControlStateNormal];
    _endDate = endDate;
}

-(void)setHotelStayResult:(HotelStayResult *)hotelStayResult{
    _hotelStayResult = hotelStayResult;
    self.hotelNameLabel.text = [NSString stringWithFormat:@"%@",hotelStayResult.hotel_name];
    self.addressLabel.text = [NSString stringWithFormat:@"地址：%@",hotelStayResult.address];
    self.telLabel.text = [NSString stringWithFormat:@"电话：%@",hotelStayResult.tel];
//    [self setBannerData:hotelStayResult.imgs];


}

-(void)setBanner{
    [self setBannerData:self.hotelStayResult.imgs];
}

//-(void)setBannerData:(NSArray *)bannerArray{
//    if (bannerArray.count==0) {
//        return;
//    }
//    
//    [self.bannerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    ImagePlayerView *imagePlayerView = [[ImagePlayerView alloc] initWithFrame:self.bannerView.bounds];
//    [imagePlayerView initWithCount:bannerArray.count delegate:self];
//    [self.bannerView addSubview:imagePlayerView];
//}

-(void)setBannerData:(NSArray *)bannerArray{
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bannerView.width, self.bannerView.height)];
    
    for (int i=0; i<bannerArray.count; i++) {
        NSString *url = bannerArray[i];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.x = i*scrollView.width;
        imageView.y = 0;
        imageView.width = scrollView.width;
        imageView.height = scrollView.height;
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:url]];
        [scrollView addSubview:imageView];
    }
    scrollView.delegate = self;
    [scrollView setContentSize:CGSizeMake(scrollView.width*bannerArray.count, scrollView.height)];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = bannerArray.count;
    pageControl.centerX = scrollView.centerX;
    pageControl.y = scrollView.height-10;
    
    [self.bannerView addSubview:scrollView];
    [self.bannerView addSubview:pageControl];
    
    self.pageControl = pageControl;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat page = scrollView.contentOffset.x/scrollView.width;
    self.pageControl.currentPage = (int)(page+0.5);
}

- (IBAction)startBtnClick:(UIButton *)sender {
    [self endEditing:YES];
    SelectDateView *dateView = [SelectDateView dateViewWithResult:^(NSString *dateStr, NSDate *date) {
        _startDate = [NSDate dateWithTimeInterval:0 sinceDate:date];
        [self.startBtn setTitle:[self stringWithDate:date] forState:UIControlStateNormal];
        
        if ([self.startDate compare:self.endDate]!=NSOrderedAscending || [[self stringWithDate:self.startDate] isEqualToString:[self stringWithDate:self.endDate]]) {
            _endDate = [self.startDate dateByAddingTimeInterval:60*60*24];
            [self.endBtn setTitle:[self stringWithDate:self.endDate] forState:UIControlStateNormal];
            
            [self setBottomLabelText];
        }
        [self.showView hideWithanim:YES];
        self.showView = nil;
    } currentDate:self.startDate minDate:[NSDate date]];
    dateView.width = SCREENW-16;
    dateView.height = 254;
    
    CustomShowView *showView = [[CustomShowView alloc] initWithCustomView:dateView];
    [showView setBackgroundColor:RGBAColor(0, 0, 0, 0.5)];
    self.showView = showView;
    [self.showView showWithanim:YES];
    
    
//    DateSelect *select = [DateSelect dateSelectWithCancelBtnClick:^{
//        [self.showView hideWithanim:YES];
//    } enterBtnClick:^(NSDate *date) {
//        [self.showView hideWithanim:YES];
//        self.startDate = [NSDate dateWithTimeInterval:0 sinceDate:date];
//        [self.startBtn setTitle:[self stringWithDate:date] forState:UIControlStateNormal];
//        
//        if ([self.startDate compare:self.endDate]!=NSOrderedAscending || [[self stringWithDate:self.startDate] isEqualToString:[self stringWithDate:self.endDate]]) {
//            self.endDate = [self.startDate dateByAddingTimeInterval:60*60*24];
//            [self.endBtn setTitle:[self stringWithDate:self.endDate] forState:UIControlStateNormal];
//            
//            [self setBottomLabelText];
//        }
//        
//    } currentDate:self.startDate minDate:[NSDate date]];
//    CustomShowView *showView = [[CustomShowView alloc]initWithCustomView:select];
//    showView.malpha = 0.3;
//    [showView showWithanim:NO];
//    self.showView = showView;
}

- (IBAction)endBtnClick:(UIButton *)sender {
    SelectDateView *dateView = [SelectDateView dateViewWithResult:^(NSString *dateStr, NSDate *date) {
        _endDate = date;
        [self.endBtn setTitle:[self stringWithDate:date] forState:UIControlStateNormal];
        [self setBottomLabelText];
        [self.showView hideWithanim:YES];
        self.showView = nil;
    } currentDate:self.endDate minDate:[self.startDate dateByAddingTimeInterval:60*60*24]];
    dateView.width = SCREENW-16;
    dateView.height = 254;
    CustomShowView *showView = [[CustomShowView alloc] initWithCustomView:dateView];
    [showView setBackgroundColor:RGBAColor(0, 0, 0, 0.5)];
    self.showView = showView;
    [self.showView showWithanim:YES];
    
//    DateSelect *select = [DateSelect dateSelectWithCancelBtnClick:^{
//        [self.showView hideWithanim:YES];
//    } enterBtnClick:^(NSDate *date) {
//        [self.showView hideWithanim:YES];
//        self.endDate = date;
//        [self.endBtn setTitle:[self stringWithDate:date] forState:UIControlStateNormal];
//        [self setBottomLabelText];
//        
//    } currentDate:self.endDate minDate:[self.startDate dateByAddingTimeInterval:60*60*24]];
//    CustomShowView *showView = [[CustomShowView alloc]initWithCustomView:select];
//    showView.malpha = 0.3;
//    [showView showWithanim:NO];
//    self.showView = showView;
}


-(void)setBottomLabelText{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:self.startDate toDate:self.endDate options:0];
    NSLog(@"components.day=%ld",(long)components.day);
    _dayCount = components.day;
    self.totalCountLabel.text = [NSString stringWithFormat:@"共%ld晚",components.day];
    
}

-(NSString *)stringWithDate:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = FMTSTR;
    return [formatter stringFromDate:date];
}


@end
